<?php

/**
 * @author aisyah
 * @copyright 2012
 */

require_once "../dao/store_dao.php";
require_once "../../app/dao/database.php";
require_once "../../config/upload_file.php";

$class_db = new Database;
$store = new Store_Dao($class_db);
$fupload = new Upload_File($dir);

$modul=$_GET['modul'];
$id=$_GET['id'];

switch($modul){
    
    case "hapus":
    
        
        
        $query = $store->db->delete("store"," store_id =".$id);
        
        if($query){
            header('location:store_view.php');            
        }else{
            header('location:store_view.php?modul=detil&id='.$id);
        }
        
    
    break;
       
    case "ubah":    
        
        $store_id = $_POST['id'];
        $store_name = $_POST['store_name'];
        $description = $_POST['description'];
        $street = $_POST['street'];
        $kecamatan = $_POST['kecamatan'];
        $city = $_POST['city'];
        $province = $_POST['province'];
        $zip_code = $_POST['zip_code'];
        $logo = $_POST['logo'];
        $image = $_POST['image'];        
        
        if(empty($_FILES['logo']['tmp_name'])){
            $fupload->upload_file('store_image','image');
            $image = $fupload->file_name;
            $row = array('store_name'=>"$store_name",'description'=>"$description",
                    'street'=>"$street",'kecamatan'=>"$kecamatan",'city'=>"$city",'province'=>"$province",
                    'zip_code'=>"$zip_code",'image'=>"$image");
                    
                    
                    
        }else if(empty($_FILES['image']['tmp_name'])){
            $fupload->upload_file('store_logo','logo');
            $logo = $fupload->file_name;
            $row = array('store_name'=>"$store_name",'description'=>"$description",
                    'street'=>"$street",'kecamatan'=>"$kecamatan",'city'=>"$city",'province'=>"$province",
                    'zip_code'=>"$zip_code",'logo'=>"$logo");
                    
        }else if(empty($_FILES['logo']['tmp_name']) AND empty($_FILES['image']['tmp_name'])){
            $row = array('store_name'=>"$store_name",'description'=>"$description",
                    'street'=>"$street",'kecamatan'=>"$kecamatan",'city'=>"$city",'province'=>"$province",
                    'zip_code'=>"$zip_code");
        }else{
            $fupload->upload_file('store_image','image');
            $image = $fupload->file_name;
            $fupload->upload_file('store_logo','logo');
            $logo = $fupload->file_name;
            $row = array('store_name'=>"$store_name",'description'=>"$description",
                    'street'=>"$street",'kecamatan'=>"$kecamatan",'city'=>"$city",'province'=>"$province",
                    'zip_code'=>"$zip_code",'logo'=>"$logo",'image'=>"$image");
                    
                }
        
        $update=$store->db->update('store',$row,'store_id='.$store_id);
        if($update){
            header('location:store_view.php');
        }else{
            echo "gak berhasil update";
        }
        
    break; 
    
    
    
    }